package com.lzh.others.designmode.factorymod.simpleFactoryMod;

/**
 * @Description: 通过反射机制改良简单工厂
 * @Author: LiuZiHao
 * @Date: 2022/11/7 14:08
 */
public class ChartFactory3 {

    public static IChart create(Class<? extends IChart> clazz) {
        IChart chart = null;
        try {
            if (clazz != null) {
                chart = clazz.newInstance();
            }
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
        return chart;
    }

    public static IChart create(String className) {
        IChart chart = null;
        try {
            if (className != null) {
                chart = (IChart) Class.forName(className).newInstance();
            }
        } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return chart;
    }
}
